package org.orbis.communicate.searcher;

import java.util.concurrent.CountDownLatch;
import org.orbis.communicate.FlagsResult;
import org.orbis.communicate.hash.Hashing;
import org.orbis.flag.SearchFlags;
import org.orbis.term.Term;
import org.orbis.term.TermList;

@Deprecated
public class SearcherPackage{
	private TermList terms;
	private CountDownLatch latch;
	private FlagsResult[][] result;
	private SearchFlags filter;
	private boolean isNotFound;

	private SearcherPackage(){}

	public static SearcherPackage newSearcher(TermList terms, SearchFlags filter, int maxSearcher){
		SearcherPackage returner = new SearcherPackage();

		returner.terms = terms;
		returner.filter = filter;
		returner.latch = new CountDownLatch(terms.size() > maxSearcher? maxSearcher: terms.size());
		returner.result = new FlagsResult[terms.size()][];
		returner.isNotFound = false;

		return returner;
	}

	public long getTermHash(int index){
		return Hashing.getTermHash(terms.get(index).str());
	}

	public int getCount(){
		return terms.size();
	}

	public Term getTerm(int index){
		return terms.get(index);
	}

	public FlagsResult[][] getAllResult(){
		return result;
	}

	public FlagsResult[] getResult(int index){
		if(index >= result.length) return null;
		return result[index];
	}

	public void setResult(int index, FlagsResult[] invIndices){
		result[index] = invIndices;
	}

	public void notFound(){
		isNotFound = true;
	}

	public boolean isNotFound(){
		return isNotFound;
	}

	public void countdown(){
		latch.countDown();
	}

	public SearchFlags getFilter(){
		return filter;
	}

	public void waitLatch(){
		try{
			latch.await();
		}catch(InterruptedException e){}
	}
}
